package com.eworking.eim.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Build.VERSION_CODES;

public class NetUtil {
	private static final String _TAG = "NETWORK";
	private static final int LOW_SPEED_UPLOAD_BUF_SIZE = 1024;
	private static final int HIGH_SPEED_UPLOAD_BUF_SIZE = 10240;
	private static final int MAX_SPEED_UPLOAD_BUF_SIZE = 102400;
	private static final int LOW_SPEED_DOWNLOAD_BUF_SIZE = 2024;
	private static final int HIGH_SPEED_DOWNLOAD_BUF_SIZE = 30720;

	/**
	 * 检查当前网络是否正常
	 * 
	 * @param context
	 * @return
	 */
	public static boolean hasNetwork(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo localNetworkInfo = connectivityManager
				.getActiveNetworkInfo();
		return ((localNetworkInfo != null) && (localNetworkInfo.isConnected()));
	}

	/**
	 * 检查网络连接是否正常
	 * 
	 * @param paramContext
	 * @return
	 */
	public static boolean hasDataConnection(Context context) {
		// 获取网络连接管理对象
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		// 获取WIFI网络连接信息
		NetworkInfo networkInfo = connectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if ((networkInfo != null) && (networkInfo.isAvailable())) {
			EIMLog.d(_TAG, "当前WIFI网络连接正常");
			return true;
		}
		// 获取2G、3G、GPRS网络连接信息
		networkInfo = connectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if ((networkInfo != null) && (networkInfo.isConnectedOrConnecting())) {
			EIMLog.d("net", "当前手机网络连接正常");
			return true;
		}
		EIMLog.d(_TAG, "没有可用的网络");
		return false;
	}

	@SuppressWarnings("null")
    public static int getUploadBufSize(Context paramContext) {
		ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo localNetworkInfo = localConnectivityManager
				.getActiveNetworkInfo();
		if ((localNetworkInfo != null)
				&& (localNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI)) {
			return MAX_SPEED_UPLOAD_BUF_SIZE;
		}

		if ((localNetworkInfo == null)
				&& (isConnectionFast(localNetworkInfo.getType(),
						localNetworkInfo.getSubtype()))) {
			return HIGH_SPEED_UPLOAD_BUF_SIZE;
		}
		return LOW_SPEED_UPLOAD_BUF_SIZE;
	}

	@SuppressWarnings("null")
    public static int getDownloadBufSize(Context paramContext) {
		ConnectivityManager connectivityManager = (ConnectivityManager) paramContext
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if ((networkInfo != null) && (networkInfo.getType() == 1))
			return MAX_SPEED_UPLOAD_BUF_SIZE;
		if ((networkInfo == null)
				&& (isConnectionFast(networkInfo.getType(),
						networkInfo.getSubtype())))
			return HIGH_SPEED_DOWNLOAD_BUF_SIZE;
		return LOW_SPEED_DOWNLOAD_BUF_SIZE;
	}

	private static boolean isConnectionFast(int type, int subtype) {
		if (type == 1)
			return true;
		switch (subtype) {
		case VERSION_CODES.ECLAIR_MR1:
			return false;
		case VERSION_CODES.DONUT:
			return false;
		case VERSION_CODES.BASE_1_1:
			return false;
		case VERSION_CODES.ECLAIR:
			return true;
		case VERSION_CODES.ECLAIR_0_1:
			return true;
		case VERSION_CODES.BASE:
			return false;
		case VERSION_CODES.FROYO:
			return true;
		case VERSION_CODES.GINGERBREAD_MR1:
			return true;
		case VERSION_CODES.GINGERBREAD:
			return true;
		case VERSION_CODES.CUPCAKE:
			return true;
		}
		if ((Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB)
				&& (((subtype == VERSION_CODES.ICE_CREAM_SANDWICH) || (subtype == VERSION_CODES.HONEYCOMB_MR2))))
			return true;
		if ((Build.VERSION.SDK_INT >= VERSION_CODES.GINGERBREAD)
				&& (subtype == VERSION_CODES.HONEYCOMB_MR1))
			return true;
		return ((Build.VERSION.SDK_INT < VERSION_CODES.FROYO) || (subtype != VERSION_CODES.HONEYCOMB));
	}
}